//
//  HWMineHotGamesView.swift
//  HWanGame
//
//  Created by 王昌健 on 2021/12/22.
//  Copyright © 2021 94Wan. All rights reserved.
//

import UIKit

class HWMineHotGamesView: UIView {
    var headerImageView:UIImageView!
    var moreButton:UIButton!
    var items:[HWMineHotGameItemView] = Array()
    var buttons:[UIButton] = Array()
    var itemButtonClick:((_ id:String)->())?
    var moreButtonClick:(()->())?
    var model:HWMineModel?{
        didSet {
            
//            headerImageView.sd_setImage(with:URL(string: model?.quanzi.icon ?? ""), completed: nil)
//            for v in items {
//                v.removeFromSuperview()
//            }
//            items.removeAll()
//            buttons.removeAll()
//            if model?.quanzi.lists.count ?? 0 > 0 {
//                var tempView:HWMineHotGameItemView!
//                for i in 0..<(model?.quanzi.lists.count ?? 0){
//                    let itemView = HWMineHotGameItemView()
//                    itemView.imageView.sd_setImage(with: URL(string: model?.quanzi.lists[i].icon ?? ""), completed: nil)
//                    itemView.nameLabel.text = model?.quanzi.lists[i].name
//                    addSubview(itemView)
//                    let button = UIButton(type: .custom)
//                    button.tag = i
//                    button.addTarget(self, action: #selector(itemButtonClick(_:)), for: .touchUpInside)
//                    itemView.addSubview(button)
//                    if tempView == nil {
//                        itemView.snp.makeConstraints { make in
//                            make.left.equalTo(0)
//                            make.top.equalTo(headerImageView.snp.bottom).offset(18*widthScale)
//                            make.width.equalTo((screenWidth-40*widthScale)/4)
//                            make.height.equalTo(87*widthScale)
//                        }
//                    }else{
//                        if i % 4 == 0 {
//                            itemView.snp.makeConstraints { make in
//                                make.left.equalTo(0)
//                                make.top.equalTo(tempView.snp.bottom)
//                                make.width.equalTo((screenWidth-40*widthScale)/4)
//                                make.height.equalTo(87*widthScale)
//                            }
//                        }else{
//                            itemView.snp.makeConstraints { make in
//                                make.left.equalTo(tempView.snp.right)
//                                make.top.equalTo(tempView)
//                                make.width.equalTo((screenWidth-40*widthScale)/4)
//                                make.height.equalTo(87*widthScale)
//                            }
//                        }
//                    }
//                    tempView = itemView
//                    if i == (model?.quanzi.lists.count ?? 0) - 1 {
//                        itemView.snp.makeConstraints { make in
//                            make.bottom.equalToSuperview()
//                        }
//                    }
//                    button.snp.makeConstraints { make in
//                        make.edges.equalToSuperview()
//                    }
//                    buttons.append(button)
//                    items.append(itemView)
//                }
//            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
    }
    
}

extension HWMineHotGamesView {
    private func initUI() -> Void {
        backgroundColor = .white
        layer.cornerRadius = 10*widthScale
        
        headerImageView = UIImageView()
        headerImageView.contentMode = .scaleAspectFit
        addSubview(headerImageView)
        
        moreButton = UIButton(type: .custom)
        moreButton.setTitle("更多", for: .normal)
        moreButton.titleLabel?.font = UIFont.init(name: "YouSheBiaoTiHei", size: 12*widthScale)!
        moreButton.setTitleColor(.colorWithHexString(hexString: "#A67A03"), for: .normal)
        moreButton.addTarget(self, action: #selector(moreButtonClick(_:)), for: .touchUpInside)
        addSubview(moreButton)
        
    }
    
    private func initFrame() -> Void {
        headerImageView.snp.makeConstraints { make in
            make.top.equalTo(14*widthScale)
            make.left.equalTo(12*widthScale)
            make.height.equalTo(26*widthScale)
            make.width.equalTo(headerImageView.snp.height).multipliedBy(3.2)
        }
        
        moreButton.snp.makeConstraints { make in
            make.right.equalTo(-12*widthScale)
            make.centerY.equalTo(headerImageView)
        }
    }
}

extension HWMineHotGamesView {
    @objc func moreButtonClick(_ button:UIButton) -> Void {
        moreButtonClick?()
    }
    
    @objc func itemButtonClick(_ button:UIButton) -> Void {
//        itemButtonClick?(self.model?.quanzi.lists[button.tag].id ?? "")
    }
    
}
